package cn.zzq.shop.common.util;

import java.io.IOException;
import java.util.Iterator;
import java.util.Random;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * 京东专门抓虫
 * @author WangshSxt
 *
 */
public class JdSpliderUtil
{
	/**
	 * 商品列表页面
	 * @return 将采集的数据以json的形式返回
	 * {
	 * 		"code":"0",
	 * 		"info":"成功",
	 * 		"data":
	 * 		{
	 * 			"goodsList":
	 * 			[
	 * 				{
	 * 					"name":"",
	 * 					"title":"",
	 * 					"price":"",
	 * 					"href":""
	 * 				}
	 * 			],
	 * 			"pageInfo":
	 * 			{
	 * 				"totalpage":""
	 * 			}
	 * 		}
	 * }
	 */
	public JSONObject list(String page)
	{
		JSONObject resultJSON = new JSONObject() ; 
		if(page == null)
		{
			page = "" ; 
		}
		if("".equalsIgnoreCase(page))
		{
			page = "1";
		}
		String url = "https://list.jd.com/list.html?cat=670,671,672&page="+ page +"&sort=sort_totalsales15_desc&trans=1&JL=6_0_0&ms=6#J_main";
		/*HttpUtil httpUtil = new HttpUtil() ;
		String res = httpUtil.methodGet(url);
		System.out.println(res);*/
		
		try
		{
			Connection conn = Jsoup.connect(url);
			/* Document ===== JS document*/
			Document document = conn.get() ;
			
			/* 获取到商品外面包着的div
			 * 目的是想获取里面所有的li
			 *  */
			Element pllistEle = document.getElementById("plist");
			/*System.out.println(pllistEle);*/
			/* 取到了第一个ul */
			Element ulEle = pllistEle.getElementsByTag("ul").get(0);
			Elements goodslisList = ulEle.getElementsByTag("li");
			
			/* 存储数据 */
			JSONArray goodsArr = new JSONArray();
			
			Random random = new Random() ; 
			
			for (Iterator iterator = goodslisList.iterator(); iterator.hasNext();)
			{
				Element goodsLi = (Element) iterator.next();
				/*System.out.println(goodsLi);*/
				
				/* 取得名称 */
				Element nameEl = goodsLi.getElementsByClass("p-name").get(0);
				/*System.out.println(pnameEls.size());*/
				
				/*System.out.println(pnameEl.getElementsByTag("a").size());*/
				Element nameAEle = nameEl.getElementsByTag("a").get(0);
				Element nameEmEle = nameAEle.getElementsByTag("em").get(0);
				
				/* 名称 */
				String name = nameEmEle.text();
				
				/* 获取属性值 */
				String href = nameAEle.attr("href");
				
				/* 图片路径 */
				Element imgEle = goodsLi.getElementsByClass("p-img").get(0);
				String imgPath = imgEle.getElementsByTag("img").get(0).attr("src");
				/*System.out.println(name + "---" + href + "--" + imgPath);*/
				
				JSONObject goodsJSON = new JSONObject() ; 
				
				goodsJSON.put("name", name);
				goodsJSON.put("href", href);
				goodsJSON.put("imgPath", imgPath);
				goodsJSON.put("price", random.nextInt(10000));
				
				goodsArr.add(goodsJSON);
				/* jdbc */
			}
			
			/* 总页数 */
			Element pageEle = document.getElementById("J_topPage");
			String totalPage = pageEle.getElementsByTag("span").get(0).getElementsByTag("i").text();
			JSONObject pageInfoJSON = new JSONObject() ; 
			pageInfoJSON.put("totalPage", totalPage);
			pageInfoJSON.put("pageSize", 60);
			
			
			JSONObject dataJSON = new JSONObject() ; 
			dataJSON.put("goodsList", goodsArr);
			dataJSON.put("pageInfo", pageInfoJSON);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		} catch (IOException e)
		{
			ConstatFinalUtil.SYS_LOGGER.error("报错了:url:{}",url,e);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.CONFIG_JSON.get("0"));
		}
		return resultJSON;
	}
	
	public static void main(String[] args)
	{
		JdSpliderUtil jdSpliderUtil = new JdSpliderUtil() ; 
		JSONObject resultJSON = jdSpliderUtil.list("");
		System.out.println(resultJSON);
	}
}
